package com.reformer.commons.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页对象
 *
 * @author sdc
 */
public class Page<T> {
    /**
     * 分页最大尺寸
     */
    public static final int MAX_PAGESIZE = 100;
    private List<T> rows;
    /**
     * 总行数
     */
    private int totalRow;
    /**
     * 每页显示条数
     */
    private int pageSize;
    /**
     * 总页码
     */
    private int totalPage;
    /**
     * 当前页
     */
    private int curPage;
    /**
     * 开始行索引
     */
    private int beginRowIndex;

    private Map<String, Object> params = new HashMap<String, Object>();

    public Page() {
    }

    /**
     * 构造分页对象
     *
     * @param totalRow 总行数
     * @param pageSize 每页显示条数
     * @param curPage  当前页
     */
    public Page(int totalRow, int pageSize, int curPage) {
        super();
        this.totalRow = totalRow;
        this.pageSize = pageSize;
        this.curPage = curPage;
    }

    /**
     * 构造分页对象
     *
     * @param pageSize 每页显示条数
     * @param curPage  当前页
     */
    public Page(int pageSize, int curPage) {
        super();
        this.pageSize = pageSize;
        this.curPage = curPage;
    }

    public int getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(int totalRow) {
        this.totalRow = totalRow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取总页码
     *
     * @return
     */
    public int getTotalPage() {
        if (totalRow > 0) {
            if (pageSize > 0) {
                return (int) Math.ceil(1.0 * totalRow / pageSize);
            } else {
                return (int) Math.ceil(1.0 * totalRow / 5);
            }
        } else {
            return totalPage;
        }

    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * 获取开始行索引
     *
     * @return
     */
    public int getBeginRowIndex() {
        if (curPage >= 1) {
            return (curPage - 1) * pageSize;
        }
        return 0;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    @Override
    public String toString() {
        return "Page [totalRow=" + totalRow + ", pageSize=" + pageSize + ", totalPage=" + totalPage + ", curPage=" + curPage + "]";
    }

}
